﻿CREATE TRIGGER tr_Page_Event
ON dbo.t_Page
FOR INSERT,UPDATE
AS

DECLARE @PageNumber int,@ImageTypeID int,@LoanID int,@UserName varchar(50)

SELECT @LoanID = t_Image.LoanID,
			 @ImageTypeID = t_Image.ImageTypeID,
			 @PageNumber = PageNumber,
			 @UserName = master.dbo.UserName()
FROM inserted INNER JOIN t_Image
ON inserted.ImageID = t_Image.ID

IF UPDATE(ImageID)
	EXEC simg_Notify @LoanID,@ImageTypeID,@PageNumber

DECLARE @Action nvarchar(2048)
DECLARE curEvent CURSOR LOCAL FOR
SELECT Action FROM t_ImageEvent WHERE ImageTypeID = @ImageTypeID

OPEN curEvent

FETCH NEXT FROM curEvent INTO @Action

WHILE @@FETCH_STATUS = 0
BEGIN
	SET @Action = 'EXEC '+ @Action + ' @LoanID,@ImageTypeID,@UserName'
	PRINT 'Action:'+@Action
	EXEC sp_executesql @Action,N'@LoanID int,@ImageTypeID int,@UserName varchar(50)',@LoanID,@ImageTypeID,@UserName
	FETCH NEXT FROM curEvent INTO @Action
END

CLOSE curEvent
DEALLOCATE curEvent

